package controller;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import org.dom4j.Document;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import java.sql.*;

public class DBUtils {
    public static String host;
    public static String port;
    public static String username;
    public static String password;
    public static String db;

//    static {
//
//        try {
//            SAXReader saxReader = new SAXReader();
//            Document document = saxReader.read("conf/db.xml");
//
//            String Xpath_host="/properties/property[@current='1']/host/text()";
//            Node node_host = document.selectSingleNode(Xpath_host);
//            host = node_host.getText();
//
//            String xpath_port = "//property[@current='1']/port/text()";
//            Node port_node = document.selectSingleNode(xpath_port);
//            port= port_node.getText();
//
//
//            String xpath_username = "//property[@current='1']/username/text()";
//            username =document.selectSingleNode(xpath_username).getStringValue();
//
//            String xpath_password = "//property[@current='1']/password/text()";
//            password = document.selectSingleNode(xpath_password).getStringValue();
//
//            String xpath_db = "//property[@current='1']/db/text()";
//            db = document.selectSingleNode(xpath_db).getStringValue();
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

    public static Connection Connection() throws Exception {


        Class.forName("com.mysql.jdbc.Driver");

        String url = "jdbc:mysql://localhost:3306/seek";
//           System.out.println(url);
        Connection connection = DriverManager.getConnection(url,"root","root");
        return connection;
    }

    public static void close(Connection connection, Statement statement, ResultSet resultSet) throws Exception {
        if (connection!=null){
            connection.close();
        }
        if (statement!=null){
            statement.close();
        }
        if (resultSet!=null){
            resultSet.close();
        }
    }

}
